package com.keba.lamp.ai.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.keba.lamp.ai.entity.*;
import com.keba.lamp.ai.service.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.keba.basic.base.service.impl.SuperServiceImpl;
import com.keba.lamp.ai.manager.AiCustomerManager;
import com.keba.lamp.ai.vo.save.AiCustomerSaveVO;
import com.keba.lamp.ai.vo.update.AiCustomerUpdateVO;
import com.keba.lamp.ai.vo.result.AiCustomerResultVO;
import com.keba.lamp.ai.vo.query.AiCustomerPageQuery;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 业务实现类
 * ai客户表
 * </p>
 *
 * @author twg
 * @date 2025-03-02 19:30:35
 * @create [2025-03-02 19:30:35] [twg] [代码生成器生成]
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class AiCustomerServiceImpl extends SuperServiceImpl<AiCustomerManager, Long, AiCustomer> implements AiCustomerService {


    private final AiApiKeyService aiApiKeyService;

    private final AiEndPointService aiEndPointService;

    private final AiApplicationService aiApplicationService;

    private final AiInvokeRecordService aiInvokeRecordService;

    private final AiInvokeFileService aiInvokeFileService;

    @Override
    protected <SaveVO> AiCustomer saveBefore(SaveVO saveVO) {
        AiCustomerSaveVO aiCustomerSaveVO = (AiCustomerSaveVO) saveVO;
        aiCustomerSaveVO.setPid(StrUtil.format("P{}", DateUtil.current() + RandomUtil.randomInt(5)));
        return super.saveBefore(aiCustomerSaveVO);
    }

    @Override
    protected <UpdateVO> AiCustomer updateBefore(UpdateVO updateVO) {
        AiCustomerUpdateVO aiCustomerUpdateVO = (AiCustomerUpdateVO) updateVO;
        aiCustomerUpdateVO.setPid(null);
        return super.updateBefore(aiCustomerUpdateVO);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeByIds(Collection<Long> idList) {
        aiApiKeyService.getSuperManager().remove(Wrappers.lambdaQuery(AiApiKey.class).in(AiApiKey::getCustomerId, idList));
        aiEndPointService.getSuperManager().remove(Wrappers.lambdaQuery(AiEndPoint.class).in(AiEndPoint::getCustomerId, idList));
        aiApplicationService.getSuperManager().remove(Wrappers.lambdaQuery(AiApplication.class).in(AiApplication::getCustomerId, idList));
        List<AiInvokeRecord> aiInvokeRecords = aiInvokeRecordService.list(Wrappers.lambdaQuery(AiInvokeRecord.class).in(AiInvokeRecord::getCustomerId, idList));
        aiInvokeRecordService.removeByIds(aiInvokeRecords.stream().map(AiInvokeRecord::getId).collect(Collectors.toList()));
        if (CollUtil.isNotEmpty(aiInvokeRecords)) {
            aiInvokeFileService.getSuperManager().remove(Wrappers.lambdaQuery(AiInvokeFile.class).in(AiInvokeFile::getInvokeRecordId, aiInvokeRecords.stream().map(AiInvokeRecord::getId).collect(Collectors.toList())));
        }
        return super.removeByIds(idList);
    }
}


